<?php

namespace app\common\model\zbtask;

use think\Model;
use traits\model\SoftDelete;

class Zbtask extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'zbtask';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'shutdownswitch_text',
        'status_text',
        'type_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getShutdownswitchList()
    {
        return ['0' => __('Shutdownswitch 0'), '1' => __('Shutdownswitch 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getShutdownswitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shutdownswitch']) ? $data['shutdownswitch'] : '');
        $list = $this->getShutdownswitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function company()
    {
        return $this->belongsTo('app\common\model\Company', 'company_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function zbcategory()
    {
        return $this->belongsTo('app\common\model\zbtask\Zbcategory', 'zbcategory_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getStatics($where = []) {
        $where['status'] = 1;
        $where['shutdownswitch'] = 1;
        $info = $this->where($where)
            ->field(['COUNT(id) as all_count', 'COUNT(type=0 OR null) as wait_check', 'COUNT(type=1 OR null) as pass_check','COUNT(type=2 OR null) as no_check'])
            ->find();

        return $info;
    }



}
